COOKIEFILE=cookie.file
URL=http://www.1channel.ch/tv-5216-Stargate-SG-1/season-4-episode-1	
	
	Pageinit=$(curl $URL)

	IFS=$'\n'
	Links=( $(echo "$Pageinit" | grep 'external.php' ) )
	Links2nd=( $(echo "$Pageinit" | grep 'external.php' ) )
	Sites=( $(echo "$Pageinit" | grep 'document.writeln') )

#echo ${#Links[@]}
#echo ${#Sites[@]}

formatarraylinks ()
{
  for (( i=0; i < ${#Links[@]}; i++ )) do
      Links[i]+=$'\n'
  done
}

initarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]=${Sites[i]:141}
      Sites[i]=${Sites[i]%??????????????????????????}
      Sites[i]+=$'\n'
  done
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]+=$'\n'
  done
  for (( j=0; j < ${#Links2nd[@]}; j++ )) do
      if [[ $(echo "${Links2nd[$j]}") == *"Part 2"* ]]; then
      #  echo "$i"
        formatarraylinks
         # Links=( ${Links[@]:0:$((i))} ${Links[@]:$((i))} )
        Links=( ${Links[@]:0:$j} ${Links[@]:$((j+1))} )
       echo ${Links[15]}
      fi
  done
} 


formatarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]+=$'\n'
  done
}      
    
  initarray
	for (( i=1; i < $((${#Sites[@]}+1)); i++ )) do
        if [[ $(echo "${Sites[$((i-1))]}") = *"Sponsor"* ]]; then                 #if [ $(echo "${Sites[$((i-1))]}") = "HD Sponsor" ] || [ $(echo "${Sites[$((i-1))]}") = " HD Sponsor" ] || [ $(echo "${Sites[$((i-1))]}") = "  HD Sponsor" ]; thenscree
          if [ $i = 1 ]; then
            Sites=( ${Sites[@]:1} )
            # echo ${Sites[@]}
            formatarray
            i=0
          fi
          if [[ $i > 1 ]]; then
            Sites=(${Sites[@]:0:$((i-1))} ${Sites[@]:$(($i))})
            formatarray
            i=0
          fi
       fi
  done

#echo ${Sites[@]}
echo ${#Links[@]}
 
 for (( i=0; i < ${#Sites[@]}; i++ )) do
      if [[ "${Sites[i]}" == *"zalaa.com"* ]]; then
        zalaa=$i
      fi
      if [[ "${Sites[i]}" == *"putlocker.com"* ]]; then
        putlocker=$i
      fi
      if [[ "${Sites[i]}" == *"filebox.com"* ]]; then
        filebox=$i
      fi
  done
  
   if [[ $zalaa != '' ]]; then
    linknum=$zalaa
  elif [[ $filebox != '' ]]; then
    linknum=$filebox
  else
    linknum=$putlocker
  fi  
  
  # catch=$(mktemp)
  # wastebin=$(wget -O /dev/null "http://www.1channel.ch/external.php?title=Dragons%27+Den&url=aHR0cDovL3d3dy5maWxlYm94LmNvbS9heXRocGd5bXNzZDI=&domain=ZmlsZWJveC5jb20=&loggedin=0" 2> $catch)
  # finallink=$(< $catch)
  # echo $finallink
  # echo ${Links[linknum]}
  # unset catch
  
  
